package com.shantoo.widget.toolbar;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shantoo.widget.R;

/**
 * 带有搜索框的ToolBar
 */
public class WidgetBar extends Toolbar {

    //WidgetBar的根视图
    private View root;
    //WidgetBar的導航按鈕
    private ImageView navigationIcon;
    //WidgetBar的标题
    private TextView mTitle;
    //WidgetBar右侧按钮
    private Button mRightButton;
    //WidgetBar的搜索框
    private EditText mSearchView;
    private LayoutInflater mInflater;

    private final static int TITLE_TEXT_SIZE_NORMAL = 18;
    private final static int PADDING_SIZE_NORMAL = 16;

    public WidgetBar(Context context) {
        this(context, null);
    }

    public WidgetBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WidgetBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();

        int padding = dp2px(PADDING_SIZE_NORMAL);

        setContentInsetsRelative(padding, padding);
        //setFitsSystemWindows(true);
        if (attrs != null) {

            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.WidgetBar, defStyleAttr, 0);

            //左侧按钮
            final Drawable navigationIcon = a.getDrawable(R.styleable.WidgetBar_navigation_icon);
            if (navigationIcon != null) {
                setNavigationIcon(navigationIcon);
            }

            final float titleTextSize = a.getDimension(1, R.styleable.WidgetBar_title_text_size);
            if (titleTextSize != 0) {
                setTitleTextSize(titleTextSize);
            }
            setTitleTextSize(TITLE_TEXT_SIZE_NORMAL);

            //右侧按钮
            final Drawable rightButtonIcon = a.getDrawable(R.styleable.WidgetBar_right_button_icon);
            if (rightButtonIcon != null) {
                setRightButtonIcon(rightButtonIcon);
            }
            CharSequence rightButtonText = a.getText(R.styleable.WidgetBar_right_button_text);
            if (rightButtonText != null) {
                setRightButtonText(rightButtonText);
            }

            //搜索框
            boolean isShowSearchView = a.getBoolean(R.styleable.WidgetBar_show_search_view, false);

            if (isShowSearchView) {
                showSearchView();
                hideTitleView();
            }
            a.recycle();
        }
    }

    /**
     * 根据手机的分辨率from dp 的单位 转成为 px(像素)
     */
    public int dp2px(float dpValue) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private void initView() {
        if (root == null) {
            mInflater = LayoutInflater.from(getContext());
            root = mInflater.inflate(R.layout.widget_toolbar, null);
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            addView(root, lp);
        }
        navigationIcon = (ImageView) root.findViewById(R.id.toolbar_navigation_icon);
        mTitle = (TextView) root.findViewById(R.id.toolbar_title);
        mSearchView = (EditText) root.findViewById(R.id.toolbar_search_view);
        mRightButton = (Button) root.findViewById(R.id.toolbar_rightButton);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setRightButtonIcon(Drawable icon) {
        if (mRightButton != null) {
            mRightButton.setBackground(icon);
            mRightButton.setVisibility(VISIBLE);
        }
    }

    @Override
    public void setNavigationIcon(Drawable resId) {
        initView();
        if (navigationIcon != null) {
            navigationIcon.setImageDrawable(resId);
        }
    }

    public void setRightButtonIcon(int icon) {
        setRightButtonIcon(getResources().getDrawable(icon));
    }

    public void setRightButtonOnClickListener(OnClickListener li) {
        mRightButton.setOnClickListener(li);
    }

    public void setRightButtonText(CharSequence text) {
        mRightButton.setText(text);
        mRightButton.setVisibility(VISIBLE);
    }

    public void setRightButtonText(int id) {
        setRightButtonText(getResources().getString(id));
    }

    public Button getRightButton() {
        return this.mRightButton;
    }

    @Override
    public void setTitle(int resId) {
        setTitle(getContext().getText(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        initView();
        if (mTitle != null) {
            mTitle.setText(title);
            showTitleView();
        }
    }

    public void setTitleTextSize(float size) {
        if (mTitle != null) {
            mTitle.setTextSize(size);
        }
    }

    @Override
    public void setTitleTextColor(@ColorInt int color) {
        if (mTitle != null) {
            mTitle.setTextColor(color);
            showTitleView();
        }
    }

    public void showSearchView() {
        if (mSearchView != null) {
            mSearchView.setVisibility(VISIBLE);
            mTitle.setVisibility(GONE);
        }
    }

    public void hideSearchView() {
        if (mSearchView != null) {
            mSearchView.setVisibility(GONE);
            mTitle.setVisibility(VISIBLE);
        }
    }

    public void showTitleView() {
        if (mTitle != null)
            mTitle.setVisibility(VISIBLE);
    }

    public void hideTitleView() {
        if (mTitle != null)
            mTitle.setVisibility(GONE);
    }
}